package com.wzq.design.business.base.user.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.SecureUtil;
import com.wzq.design.base.dto.QueryByIdRequest;
import com.wzq.design.base.service.CommService;
import com.wzq.design.business.base.user.dto.BaseUserDto;
import com.wzq.design.business.base.user.dto.UserRequest;
import com.wzq.design.business.base.user.entity.BaseUser;
import com.wzq.design.business.base.user.mapper.UserMapper;
import com.wzq.design.config.BaseConfig;
import com.wzq.ssm.parent.base.dto.Page;
import com.wzq.ssm.parent.util.EntityUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author wangzhiqiang
 * @since 2023/5/8 15:05
 */
@Service
public class UserService extends CommService<UserMapper, BaseUser> {

    private final UserMapper userMapper;

    private final BaseConfig baseConfig;

    public UserService(UserMapper userMapper, BaseConfig baseConfig) {
        this.userMapper = userMapper;
        this.baseConfig = baseConfig;
    }

    @Transactional
    public BaseUserDto save(BaseUserDto dto) {
        BaseUser baseUser;
        if (StringUtils.isBlank(dto.getId())) {
            baseUser = EntityUtil.castDtoToModel(dto, BaseUser.class);
            if (StringUtils.isNotBlank(baseUser.getLoginName())) {
                baseUser.setSalt(UUID.fastUUID().toString());
                baseUser.setPassword(SecureUtil.pbkdf2(baseConfig.getDefaultPwd().toCharArray(), baseUser.getSalt().getBytes()));
            }
            userMapper.insert(baseUser);
        } else {
            baseUser = userMapper.selectById(dto.getId());
            if (StringUtils.isBlank(baseUser.getLoginName()) && StringUtils.isNotBlank(dto.getLoginName())) {
                baseUser.setSalt(UUID.fastUUID().toString());
                baseUser.setPassword(SecureUtil.pbkdf2(baseConfig.getDefaultPwd().toCharArray(), baseUser.getSalt().getBytes()));
            }
            EntityUtil.copyDtoToBean(dto, baseUser);
            userMapper.updateById(baseUser);
        }

        return EntityUtil.castDtoToModel(baseUser, BaseUserDto.class);
    }

    @Transactional
    public void resetPassword(BaseUserDto dto) {
        BaseUser baseUser = userMapper.selectById(dto.getId());
        Assert.notBlank(baseUser.getLoginName(), "用户名不存在");
        baseUser.setSalt(UUID.fastUUID().toString());
        baseUser.setPassword(SecureUtil.pbkdf2(baseConfig.getDefaultPwd().toCharArray(), baseUser.getSalt().getBytes()));
        userMapper.updateById(baseUser);
    }

    public Page<BaseUserDto> list(UserRequest userRequest) {
        Page<BaseUser> baseUsers = userMapper.listUser( userRequest);
        return Page.create(userRequest, EntityUtil.castDtoToModel(baseUsers.getRecords(), BaseUserDto.class));
    }

    public BaseUserDto findById(QueryByIdRequest userRequest) {
        BaseUser user = userMapper.selectById(userRequest.getId());
        return EntityUtil.castDtoToModel(user, BaseUserDto.class);
    }

}
